package org.linlinjava.litemall.brand.web;

import com.github.pagehelper.PageInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.brand.annotation.LoginUser;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallCommodity;
import org.linlinjava.litemall.db.domain.LitemallCommodityWithBLOBs;
import org.linlinjava.litemall.db.service.LitemallCommodityService;
import org.linlinjava.litemall.db.service.LitemallServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @program: mall
 * @author: youvy
 * @create: 2019-11-22 19:47
 **/
@RestController
@RequestMapping("/brand/commodity")
public class BrandCommodityController {
    private final Log logger = LogFactory.getLog(BrandController.class);
	@Autowired
	LitemallCommodityService commodityService;
	@Autowired
	LitemallServiceService serviceService;


	@PostMapping("list")
	public Object list(@RequestBody String msg, @LoginUser Integer userId) {
		Integer page = JacksonUtil.parseInteger(msg, "page");
		Integer pageSize = JacksonUtil.parseInteger(msg, "pageSize");
		Integer categoryPid = JacksonUtil.parseInteger(msg, "categoryPid");
		List<Integer> categoryId = JacksonUtil.parseIntegerList(msg, "categoryId");
		Integer brandId = serviceService.getBelong(userId);
		List<Integer> brandIdList = new LinkedList<>();
		brandIdList.add(brandId);
		if (page == null || pageSize == null) {
			return ResponseUtil.badArgument();
		}
		if (page < 0 || pageSize < 0) {
			return ResponseUtil.badArgumentValue();
		}
		PageInfo<LitemallCommodity> pageInfo = commodityService.selectByCondition(page, pageSize, categoryPid, categoryId, brandIdList,null,null);
		Map<String, Object> map = new LinkedHashMap<>(2);
		map.put("totalNum", pageInfo.getTotal());
		map.put("list", pageInfo.getList());
		return ResponseUtil.ok(map);
	}
	@PostMapping("detail")
	public Object detail(@RequestBody String msg) {
		Integer id = JacksonUtil.parseInteger(msg, "id");
		if (id == null) {
			return ResponseUtil.badArgument();
		}
		if (id < 0) {
			return ResponseUtil.badArgumentValue();
		}
		return ResponseUtil.ok(commodityService.selectById(id));
	}

	/**
	 * 新增商品
	 * @param commodity
	 * @param userId
	 * @return
	 */
	@PostMapping("insert")
	public Object insertCommodity(@RequestBody LitemallCommodityWithBLOBs commodity, @LoginUser Integer userId){
		//commodity.setCommodityPrice(commodity.getCommodityPrice().setScale(2, RoundingMode.HALF_UP));
		commodity.setBrandId(serviceService.getBelong(userId));
		int i = commodityService.insertCommodity(commodity);
		if (i>0){
			return ResponseUtil.ok();
		}else {
			return ResponseUtil.badArgument();
		}
	}
	@PostMapping("update")
	public Object updateCommodity(@RequestBody LitemallCommodityWithBLOBs commodity){
		int i = commodityService.updateCommodity(commodity);
		if (i>0){
			return ResponseUtil.ok();
		}else {
			return ResponseUtil.badArgument();
		}
	}
	@PostMapping("delete")
	public Object deleteCommodity(@RequestBody String msg){
		Integer id = JacksonUtil.parseInteger(msg, "id");
		if (id == null || id < 1) {
			return ResponseUtil.badArgument();
		}
		int i = commodityService.deleteCommodity(id);
		if (i>0){
			return ResponseUtil.ok();
		}else {
			return ResponseUtil.badArgument();
		}
	}


}
